/* Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/*
  <file> line.c </file>
  <brief>
    Testing input stream of which source is char array with length.
    This file is part of ULS, Unified Lexical Scheme.
  </brief>
  <author>
    Stanley J. Hong <babel96@gmail.com>, 2011.
  </author>
*/

#include "uls.h"

#include "sample_lex.h"
#include "uls.h"

#include <unistd.h>
#include <getopt.h>
#include <fcntl.h>
#include <time.h>

char *progname;
int  opt_verbose;

char *config_name;
char *input_file;
uls_lex_t *sample_lex;

int shell_proc_file(char* fname);

static void usage(void)
{
	err_log("%s v1.0", progname);
	err_log("  Dumps tokens in inputfiles");
	err_log("  according to the token defintions in %s", config_name);
	err_log("  Uses uls_set_line() to set the input source");
	err_log("");
	err_log(" Usage:");
	err_log("\t%s [-c <config-file>]  <inputfile-1> ...", progname);
	err_log("");
	err_log("  For example,");
	err_log("      %s input1.txt", progname);
	err_log("  A default config-file used, %s if you don't specifiy the config-file.", config_name);
	exit(1);
}

static int
parse_options(int argc, char* argv[])
{
	int   opt;

	while ((opt=getopt(argc, argv, "c:vh")) != -1) {
		switch (opt) {
		case 'c':
			config_name = optarg;
			break;

		case 'v':
			opt_verbose = 1;
			break;

		case 'h':
			usage();
			break;

		default:
			err_log("undefined option -%c", opt);
			return -1;
			break;
		}
	}

	return optind;
}

int
main(int argc, char* argv[])
{
	int i, i0;

	progname = uls_filename(argv[0]);
	config_name = "sample.ulc";

	if ((i0=parse_options(argc, argv)) < 0 || i0 >= argc) {
		usage();
		return -1;
	}

	if ((sample_lex = uls_create(config_name)) == NULL) {
		err_log("can't create uls-lexer");
		return -1;
	}

	for (i=i0; i<argc; i++) {
		// shell style
		input_file = argv[i];

		uls_set_tag(sample_lex, input_file);
		shell_proc_file(input_file);
	}

	uls_destroy(sample_lex);

	return 0;
}

static FILE   *cur_fin;
static char   filebuff[FILEBUFF_SIZ+1];

int
shell_proc_file(char* fname)
{
	int lno = 0;

	if ((cur_fin=fopen(fname, "r"))==NULL) {
		err_log("%s: fail to open '%s'", __FUNCTION__, fname);
		return -1;
	}

	while (fgets(filebuff, FILEBUFF_SIZ, cur_fin) != NULL) {
		uls_set_line(sample_lex, filebuff, strlen(filebuff), 0);
		uls_inc_lineno(sample_lex, ++lno);

		while (uls_gettok(sample_lex) != tokEOI) {
			uls_prntok(sample_lex);
		}
	}

	fclose(cur_fin);

	return 0;
}
